package com.lvmama.pangolin.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lvmama.pangolin.entity.mt.MTTicketCommodityItem;
import com.lvmama.pangolin.entity.mt.MTTicketProduct;
import com.lvmama.pangolin.mt.service.MTTicketCommodityItemsService;
import com.lvmama.pangolin.mt.service.MTTicketProductService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lvmama.pangolin.HBaseQueryPrice.HBaseHotelHistoryPrice;
import com.lvmama.pangolin.HBaseQueryPrice.HBaseTicketHistoryPrice;
import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.entity.LineDataVO;
import com.lvmama.pangolin.entity.RelevanceProduct;
import com.lvmama.pangolin.entity.hotel.HotelCommodityDetail;
import com.lvmama.pangolin.entity.link.HotelCommodityShowView;
import com.lvmama.pangolin.entity.link.TicketCommodityLink;
import com.lvmama.pangolin.entity.link.TicketCommodityShowView;
import com.lvmama.pangolin.entity.link.TicketProductLinkView;
import com.lvmama.pangolin.entity.lmm.LMMTicketCommodity;
import com.lvmama.pangolin.entity.lmm.LMMTicketProduct;
import com.lvmama.pangolin.entity.qnr.QNRTicketCommodityItem;
import com.lvmama.pangolin.entity.qnr.QNRTicketProduct;
import com.lvmama.pangolin.entity.tc.TCTicketProduct;
import com.lvmama.pangolin.entity.tc.TCommodityItem;
import com.lvmama.pangolin.entity.tn.TNTicketCommodityItem;
import com.lvmama.pangolin.entity.tn.TNTicketProduct;
import com.lvmama.pangolin.entity.xc.XCTicketCommodityItem;
import com.lvmama.pangolin.entity.xc.XCTicketProduct;
import com.lvmama.pangolin.hotel.service.HotelCommodityDetailService;
import com.lvmama.pangolin.lmm.service.LMMTicketCommodityService;
import com.lvmama.pangolin.lmm.service.LMMTicketProductService;
import com.lvmama.pangolin.qnr.service.QNRTicketCommodityItemsService;
import com.lvmama.pangolin.qnr.service.QNRTicketProductService;
import com.lvmama.pangolin.service.RelevanceCommodityService;
import com.lvmama.pangolin.service.RelevanceProductService;
import com.lvmama.pangolin.tc.service.TCCommodityItemsService;
import com.lvmama.pangolin.tc.service.TCTicketProductService;
import com.lvmama.pangolin.tn.service.TNTicketCommodityItemsService;
import com.lvmama.pangolin.tn.service.TNTicketProductService;
import com.lvmama.pangolin.xc.service.XCTicketCommodityItemsService;
import com.lvmama.pangolin.xc.service.XCTicketProductService;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("/datashow")
public class ShowCommodityController {

	@Autowired
	private RelevanceCommodityService relevanceCommodityService;
	@Autowired
	private LMMTicketCommodityService lmmCommodityService;
	@Autowired
	private XCTicketCommodityItemsService xcCommodityService;
	@Autowired
	private TNTicketCommodityItemsService tnCommodityService;
	@Autowired
	private TCCommodityItemsService tcCommodityService;
	@Autowired
	private QNRTicketCommodityItemsService qnrCommodityService;
	@Autowired
	private MTTicketCommodityItemsService mtCommodityService;
	@Autowired
	private HBaseHotelHistoryPrice historyHotelPrice;
	@Autowired
	private HBaseTicketHistoryPrice historyTicketPrice;
	@Autowired
	private RelevanceProductService relevanceProductService;
	@Autowired
	private LMMTicketProductService lProductService;
	@Autowired
	private XCTicketProductService xcProductService;
	@Autowired
	private TNTicketProductService tnProductService;
	@Autowired
	private TCTicketProductService tcProductService;
	@Autowired
	private QNRTicketProductService qnrProductService;
	@Autowired
	private MTTicketProductService mtProductService;
	@Autowired
	private HotelCommodityDetailService hotelCommodityService;
	/**
	 * 商品展示
	 * @param
	 * @param
	 * @param
	 * @return
	 */
	@RequestMapping("/show_product")
	@ResponseBody
	public Object showProduct(String prod_id,String businessCode){//产品展示主界面，商品列表业务逻辑处理
		Map<String, Object> result = getResult();
		TicketCommodityLink comLink=new TicketCommodityLink();
		comLink.setProd_id(prod_id);
		
		/**
		 * 获取所有关联的商品id
		 */
		List<String> commodityIds= relevanceCommodityService.groupByCommodityId(prod_id,businessCode);
		if(commodityIds.size()>0){
			
			if(businessCode==null||"".equals(businessCode)||"TICKET".equals(businessCode)){
				/**
				 * 创建list用来展示页面数据
				 */
				List<TicketCommodityShowView> tcsvs=new ArrayList<TicketCommodityShowView>();
				for (int i=0;i<commodityIds.size();i++){
					String comId=commodityIds.get(i);
					comLink.setCommodity_id(comId);
					/**
					 * 获取一个商品的全部关联信息
					 */
					List<TicketCommodityLink> tcls=relevanceCommodityService.viewCommodityLink(comLink);
					
					/**
					 * 创建对象，用来存放商品展示页面的一条记录
					 */
					TicketCommodityShowView tcsv=new TicketCommodityShowView();
					/**
					 * 根据商品id获取lmm商品的详细信息，并封装到页面记录中
					 */
					LMMTicketCommodity lc=lmmCommodityService.selectByProdCommId(prod_id,comId);
					tcsv.setProd_id(lc.getProductId());
					tcsv.setCommodity_id(lc.getCommodityId());
					tcsv.setCommodity_name(lc.getCommodityName());
					tcsv.setSale_price(lc.getSalePrice());
					tcsv.setSale_cond(lc.getSaleCond());
					tcsv.setRefund_cond(lc.getRefundCond());
					tcsv.setPreferential_info(lc.getPreferentialInfo());
					tcsv.setSet_as_lowest(lc.getSet_as_lowest());
					for(int j=0;j<tcls.size();j++){
						TicketCommodityLink tcl=tcls.get(j);
						if("xc".equals(tcl.getCompany_code())){
							/**
							 * 根据商品id获取xc商品的详细信息，并封装到页面记录中
							 */
							XCTicketCommodityItem xcc=xcCommodityService.selectByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id());
							tcsv.setPreferential_info_xc(xcc.getPreferential());
							tcsv.setRefund_cond_xc(xcc.getRefund_cond());
							tcsv.setSale_cond_xc(xcc.getSale_cond());
							tcsv.setSale_price_xc(xcc.getSale_price());
							tcsv.setCommodity_name_xc(xcc.getCommodity_name());
							
						}else if("tn".equals(tcl.getCompany_code())){
							/**
							 * 根据商品id获取tn商品的详细信息，并封装到页面记录中
							 */
							TNTicketCommodityItem tnc=tnCommodityService.selectByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id());
							tcsv.setPreferential_info_tn(tnc.getPreferential());
							tcsv.setRefund_cond_tn(tnc.getRefund_cond());
							tcsv.setSale_cond_tn(tnc.getSale_cond());
							tcsv.setSale_price_tn(tnc.getSale_price());
							tcsv.setCommodity_name_tn(tnc.getCommodity_name());
							
						}else if("tc".equals(tcl.getCompany_code())){
							/**
							 * 根据商品id获取tc商品的详细信息，并封装到页面记录中
							 */
							TCommodityItem tcc=tcCommodityService.selectByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id());
							if(tcc!=null){
								tcsv.setPreferential_info_tc(tcc.getPreferential()==null?"":tcc.getPreferential());
								tcsv.setRefund_cond_tc(tcc.getRefund_cond());
								tcsv.setSale_cond_tc(tcc.getSale_cond());
								tcsv.setSale_price_tc(tcc.getSale_price());
								tcsv.setCommodity_name_tc(tcc.getCommodity_name());
							}
							
						}else if("qnr".equals(tcl.getCompany_code())){
							/**
							 * 根据商品id获取qnr商品的详细信息，并封装到页面记录中
							 */
							QNRTicketCommodityItem qnrc=qnrCommodityService.selectByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id());
							tcsv.setPreferential_info_qnr(qnrc.getPreferential());
							tcsv.setRefund_cond_qnr(qnrc.getRefund_cond());
							tcsv.setSale_cond_qnr(qnrc.getSale_cond());
							tcsv.setSale_price_qnr(qnrc.getSale_price());
							tcsv.setCommodity_name_qnr(qnrc.getCommodity_name());
						}//新增美团数据
						else if("mt".equals(tcl.getCompany_code())){
							MTTicketCommodityItem mtc=mtCommodityService.selectByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id());
							tcsv.setPreferential_info_mt(mtc.getPreferential());
							tcsv.setRefund_cond_mt(mtc.getRefund_cond());
							tcsv.setSale_cond_mt(mtc.getSale_cond());
							tcsv.setSale_price_mt(mtc.getSale_price());
							tcsv.setCommodity_name_mt(mtc.getCommodity_name());
						}
						
					}
					tcsvs.add(tcsv);
				}
				result.put("tcsvs",tcsvs);
			}else if("HOTEL".equals(businessCode)){
				/**
				 * 创建list用来展示页面数据
				 */
				List<HotelCommodityShowView> tcsvs=new ArrayList<HotelCommodityShowView>();
				for(int i=0;i<commodityIds.size();i++){
					String comId=commodityIds.get(i);
					comLink.setCommodity_id(comId);
					/**
					 * 获取一个商品的全部关联信息
					 */
					List<TicketCommodityLink> tcls=relevanceCommodityService.viewCommodityLink(comLink);
					
					/**
					 * 创建对象，用来存放商品展示页面的一条记录
					 */
					HotelCommodityShowView tcsv=new HotelCommodityShowView();
					/**
					 * 根据商品id获取lmm商品的详细信息，并封装到页面记录中
					 */
					HotelCommodityDetail lmmHotel=hotelCommodityService.findByProdCommId(prod_id, comId, "LMM_C_CODE");
					if(lmmHotel!=null){
						tcsv.setProd_id(lmmHotel.getProductId());
						tcsv.setCommodity_id(lmmHotel.getCommodityId());
						tcsv.setCommodity_name(lmmHotel.getCommodityName());
						tcsv.setSale_price(lmmHotel.getSalePrice());
						tcsv.setRefund_cond(lmmHotel.getCancelRule());
						tcsv.setPreferential_info(lmmHotel.getPreferential());
						tcsv.setArea(lmmHotel.getArea());
						tcsv.setBed_type(lmmHotel.getBedType());
						tcsv.setBreakfast(lmmHotel.getBreakfast());
						tcsv.setBroadband(lmmHotel.getBroadband());
						tcsv.setCommodity_type(lmmHotel.getCommodityType());
//						tcsv.setCommodity_type(lmmHotel.getRoomType());
					}
					
					for(int j=0;j<tcls.size();j++){
						TicketCommodityLink tcl=tcls.get(j);
						if("xc".equals(tcl.getCompany_code())){
							/**
							 * 根据商品id获取xc商品的详细信息，并封装到页面记录中
							 */
							HotelCommodityDetail xcHotel=hotelCommodityService.findByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id(), "XC_C_CODE");
							tcsv.setPreferential_info_xc(xcHotel.getPreferential());
							tcsv.setRefund_cond_xc(xcHotel.getCancelRule());
							tcsv.setSale_price_xc(xcHotel.getSalePrice());
							tcsv.setCommodity_name_xc(xcHotel.getCommodityName());
							tcsv.setArea_xc(xcHotel.getArea());
							tcsv.setBed_type_xc(xcHotel.getBedType());
							tcsv.setBreakfast_xc(xcHotel.getBreakfast());
							tcsv.setBroadband_xc(xcHotel.getBroadband());
							tcsv.setCommodity_type_xc(xcHotel.getCommodityType());
//							tcsv.setCommodity_type_xc(xcHotel.getRoomType());
						}else if("tn".equals(tcl.getCompany_code())){
							/**
							 * 根据商品id获取tn商品的详细信息，并封装到页面记录中
							 */
							HotelCommodityDetail tnHotel=hotelCommodityService.findByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id(), "TN_C_CODE");
							tcsv.setPreferential_info_tn(tnHotel.getPreferential());
							tcsv.setRefund_cond_tn(tnHotel.getCancelRule());
							tcsv.setSale_price_tn(tnHotel.getSalePrice());
							tcsv.setCommodity_name_tn(tnHotel.getCommodityName());
							tcsv.setArea_tn(tnHotel.getArea());
							tcsv.setBed_type_tn(tnHotel.getBedType());
							tcsv.setBreakfast_tn(tnHotel.getBreakfast());
							tcsv.setBroadband_tn(tnHotel.getBroadband());
//							tcsv.setCommodity_type_tn(tnHotel.getRoomType());
							tcsv.setCommodity_type_tn(tnHotel.getCommodityType());
						}/*else if("tc".equals(tcl.getCompany_code())){
							
							HotelCommodityDetail tcHotel=hotelCommodityService.findByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id(), "TC_C_CODE");
							tcsv.setPreferential_info_tc(tcHotel.getPreferential());
							tcsv.setRefund_cond_tc(tcHotel.getCancelRule());
							tcsv.setSale_price_tc(tcHotel.getSalePrice());
							tcsv.setCommodity_name_tc(tcHotel.getCommodityName());
							
						}*/else if("qnr".equals(tcl.getCompany_code())){
							/**
							 * 根据商品id获取qnr商品的详细信息，并封装到页面记录中
							 */
							HotelCommodityDetail qnrHotel=hotelCommodityService.findByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id(), "QNR_C_CODE");
							tcsv.setPreferential_info_qnr(qnrHotel.getPreferential());
							tcsv.setRefund_cond_qnr(qnrHotel.getCancelRule());
							tcsv.setSale_price_qnr(qnrHotel.getSalePrice());
							tcsv.setCommodity_name_qnr(qnrHotel.getCommodityName());
							tcsv.setArea_qnr(qnrHotel.getArea());
							tcsv.setBed_type_qnr(qnrHotel.getBedType());
							tcsv.setBreakfast_qnr(qnrHotel.getBreakfast());
							tcsv.setBroadband_qnr(qnrHotel.getBroadband());
//							tcsv.setCommodity_type_qnr(qnrHotel.getRoomType());
							tcsv.setCommodity_type_qnr(qnrHotel.getCommodityType());
						}else if("el".equals(tcl.getCompany_code())){
							/**
							 * 根据商品id获取el商品的详细信息，并封装到页面记录中
							 */
							HotelCommodityDetail elHotel=hotelCommodityService.findByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id(), "EL_C_CODE");
							tcsv.setPreferential_info_el(elHotel.getPreferential());
							tcsv.setRefund_cond_el(elHotel.getCancelRule());
							tcsv.setSale_price_el(elHotel.getSalePrice());
							tcsv.setCommodity_name_el(elHotel.getCommodityName());
							tcsv.setArea_el(elHotel.getArea());
							tcsv.setBed_type_el(elHotel.getBedType());
							tcsv.setBreakfast_el(elHotel.getBreakfast());
							tcsv.setBroadband_el(elHotel.getBroadband());
//							tcsv.setCommodity_type_el(elHotel.getRoomType());
							tcsv.setCommodity_type_el(elHotel.getCommodityType());
						}else if("mt".equals(tcl.getCompany_code())){//新增美团
							HotelCommodityDetail mtHotel=hotelCommodityService.findByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id(),"MT_C_CODE");
							tcsv.setPreferential_info_mt(mtHotel.getPreferential());
							tcsv.setRefund_cond_mt(mtHotel.getCancelRule());//取消规则
							tcsv.setSale_price_mt(mtHotel.getSalePrice());
							tcsv.setCommodity_name_el(mtHotel.getCommodityName());
							tcsv.setArea_mt(mtHotel.getArea());
							tcsv.setBed_type_mt(mtHotel.getBedType());
							tcsv.setBreakfast_mt(mtHotel.getBreakfast());
							tcsv.setBroadband_mt(mtHotel.getBroadband());
//							tcsv.setCommodity_type_mt(mtHotel.getRoomType());
							tcsv.setCommodity_type_mt(mtHotel.getCommodityType());
						}
					}
					tcsvs.add(tcsv);
				}
				result.put("tcsvs",tcsvs);
			}
			
		}else{
			result.put("number", 0);
		}
		
		return result;
	}
	
	/**
	 * 异步请求历史商品价格
	 * @param productId
	 * @return
	 */
	@RequestMapping("/commodityPriceHistory")
	@ResponseBody
	public Object commodityPriceHistory(String productId,String commodityId,String chooseTime,String businessCode){
		Map<String, Object> result = getResult();
		TicketCommodityLink comLink=new TicketCommodityLink();
		comLink.setCommodity_id(commodityId);
		comLink.setProd_id(productId);
		List<LineDataVO>priceList=new ArrayList<LineDataVO>();
		RelevanceProduct rp=relevanceProductService.viewRelevance(productId,businessCode);
		/**
		 *获取商品id对应的关联信息
		 */
		List<TicketCommodityLink>  tcls=relevanceCommodityService.viewCommodityLink(comLink);
		/**
		 * 根据商品id查到lmm的商品信息
		 */
		if(businessCode==null||"".equals(businessCode)||"TICKET".equals(businessCode)){
			String tableNamel=Constant.TICKET_COMMODITY_H_LMM;
			String family=Constant.HB_VARIABLE_FAMILY;
			List<Map<String,String>> lmmList=historyTicketPrice.queryCommodityByDate(chooseTime, commodityId, tableNamel, family);
			Collections.sort(lmmList,new Comparator<Map<String,String>>() {
				@Override
				public int compare(Map<String, String> o1, Map<String, String> o2) {
					return o1.get("insert_time").compareTo(o2.get("insert_time"));
				}
				
			});
			parseToLineDataVo(lmmList,"lmm",priceList);
			for(int j=0;j<tcls.size();j++){
				TicketCommodityLink tcl=tcls.get(j);
				if("xc".equals(tcl.getCompany_code())){
//					XCTicketCommodityItem xcc=xcCommodityService.selectByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id());
					String tableNamex=Constant.TICKET_COMMODITY_H_XC;
					List<Map<String,String>> xcList=historyTicketPrice.queryCommodityByDate(chooseTime, tcl.getRel_commodity_id(), tableNamex, family);
					Collections.sort(xcList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
							return o1.get("create_time").compareTo(o2.get("create_time"));
						}
						
					});
					parseToLineDataVo(xcList,"xc",priceList);
				}else if("tn".equals(tcl.getCompany_code())){
//					TNTicketCommodityItem tnc=tnCommodityService.selectByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id());
					String tableNamet=Constant.TICKET_COMMODITY_H_TN;
					List<Map<String,String>> tnList=historyTicketPrice.queryCommodityByDate(chooseTime, tcl.getRel_commodity_id(), tableNamet, family);
					Collections.sort(tnList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
							return o1.get("create_time").compareTo(o2.get("create_time"));
						}
						
					});
					parseToLineDataVo(tnList,"tn",priceList);
				}else if("tc".equals(tcl.getCompany_code())){
					String tableNametc=Constant.TICKET_COMMODITY_H_TC;
					List<Map<String,String>> tcList=historyTicketPrice.queryCommodityByDate(chooseTime, tcl.getRel_commodity_id(), tableNametc, family);
					Collections.sort(tcList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
							return o1.get("create_time").compareTo(o2.get("create_time"));
						}
						
					});
					parseToLineDataVo(tcList,"tc",priceList);
				}else if("qnr".equals(tcl.getCompany_code())){

//					MTTicketCommodityItem qnrc=qnrCommodityService.selectByProdCommId(tcl.getRel_prod_id(),tcl.getRel_commodity_id());
					String tableNameq=Constant.TICKET_COMMODITY_H_QNR;
					List<Map<String,String>> qnrList=historyTicketPrice.queryCommodityByDate(chooseTime, tcl.getRel_commodity_id(), tableNameq, family);
					Collections.sort(qnrList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
							return o1.get("create_time").compareTo(o2.get("create_time"));
						}
						
					});
					result.put("qnrList",qnrList);
					parseToLineDataVo(qnrList,"qnr",priceList);
				}else if("mt".equals(tcl.getCompany_code())){//增加美团
					String tableName=Constant.TICKET_COMMODITY_H_MT;
					List<Map<String,String>> mtList=historyTicketPrice.queryCommodityByDate(chooseTime,tcl.getRel_commodity_id(),tableName,family);
					if(mtList!=null&&mtList.size()>0){
						Collections.sort(mtList,new Comparator<Map<String,String>>() {
							@Override
							public int compare(Map<String, String> o1, Map<String, String> o2) {
								if(o1==null|| StringUtils.isEmpty(o1.get("create_time")))
									return -1;
								if(o2==null|| StringUtils.isEmpty(o2.get("create_time")))
									return 1;
								return o1.get("create_time").compareTo(o2.get("create_time"));
							}

						});
					}
					result.put("mtList",mtList);
					parseToLineDataVo(mtList,"mt",priceList);

				}
			}
		}else if("HOTEL".equals(businessCode)){
			String tableNamel=Constant.HOTEL_COMMODITY_H;
			List<Map<String,String>> lmmList=historyHotelPrice.queryHotelCommodityByRange(commodityId,productId,"LMM_C_CODE", tableNamel,chooseTime);
			Collections.sort(lmmList,new Comparator<Map<String,String>>() {
				@Override
				public int compare(Map<String, String> o1, Map<String, String> o2) {
					return o1.get("updateTime").compareTo(o2.get("updateTime"));
				}
				
			});
			parseToLineDataVo(lmmList,"lmm",priceList);
			for(int j=0;j<tcls.size();j++){
				TicketCommodityLink tcl=tcls.get(j);
				if("xc".equals(tcl.getCompany_code())){
					List<Map<String,String>> xcList=historyHotelPrice.queryHotelCommodityByRange(tcl.getRel_commodity_id(),rp.getProdIdXC(),"XC_C_CODE", tableNamel,chooseTime);
					Collections.sort(xcList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
							return o1.get("updateTime").compareTo(o2.get("updateTime"));
						}
						
					});
					parseToLineDataVo(xcList,"xc",priceList);
				}else if("tn".equals(tcl.getCompany_code())){
					List<Map<String,String>> tnList=historyHotelPrice.queryHotelCommodityByRange(tcl.getRel_commodity_id(),rp.getProdIdTN(),"TN_C_CODE", tableNamel,chooseTime);
					Collections.sort(tnList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
							return o1.get("updateTime").compareTo(o2.get("updateTime"));
						}
						
					});
					parseToLineDataVo(tnList,"tn",priceList);
				}else if("tc".equals(tcl.getCompany_code())){
					List<Map<String,String>> tcList=historyHotelPrice.queryHotelCommodityByRange(tcl.getRel_commodity_id(),rp.getProdIdTC(),"TC_C_CODE", tableNamel,chooseTime);
					Collections.sort(tcList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
							return o1.get("updateTime").compareTo(o2.get("updateTime"));
						}
						
					});
					parseToLineDataVo(tcList,"tc",priceList);
				}else if("qnr".equals(tcl.getCompany_code())){

					List<Map<String,String>> qnrList=historyHotelPrice.queryHotelCommodityByRange(tcl.getRel_commodity_id(),rp.getProdIdQNR(),"QNR_C_CODE", tableNamel,chooseTime);
					Collections.sort(qnrList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
							return o1.get("updateTime").compareTo(o2.get("updateTime"));
						}
						
					});
					result.put("qnrList",qnrList);
					parseToLineDataVo(qnrList,"qnr",priceList);
				}else if("el".equals(tcl.getCompany_code())){

					List<Map<String,String>> elList=historyHotelPrice.queryHotelCommodityByRange(tcl.getRel_commodity_id(),rp.getProdIdEL(),"EL_C_CODE", tableNamel,chooseTime);
					Collections.sort(elList,new Comparator<Map<String,String>>() {
						@Override
						public int compare(Map<String, String> o1, Map<String, String> o2) {
							return o1.get("updateTime").compareTo(o2.get("updateTime"));
						}
						
					});
					result.put("elList",elList);
					parseToLineDataVo(elList,"el",priceList);
				}
			}
		}
		JSONArray priceData = JSONArray.fromObject(priceList);
		result.put("priceData", priceData);
		return result;
	}
	@RequestMapping("/queryCommodityLink")
	@ResponseBody
	public Object queryCommodityLink(TicketCommodityLink comLink){
		List<TicketCommodityLink> tcls=relevanceCommodityService.viewCommodityLink(comLink);
		Map<String, Object> result = getResult();
		result.put("number", tcls.size());
		return result;
	}
	
	@RequestMapping("/setAsLowest")
	@ResponseBody
	public Object setAsLowest(TicketCommodityLink comLink){
		Map<String, Object> result=getResult();

		if(comLink!=null&& comLink.getProd_id()!=null&&comLink.getCommodity_id()!=null)
		{
			lmmCommodityService.deleteLowest(comLink.getProd_id());
			lmmCommodityService.setAsLowest(comLink.getProd_id(), comLink.getCommodity_id());
			result.put("msg", "设置成功");
		}else
		{
			result.put("msg","参数传入异常！");
		}

		return result;
	}
	@RequestMapping("/deleteLowest")
	@ResponseBody
	public Object deleteLowest(TicketCommodityLink comLink,String businessCode){
		Map<String, Object> result=getResult();
		lmmCommodityService.deleteLowest(comLink.getProd_id());
		/**
		 * 用来存放一条展示信息
		 */
		TicketProductLinkView tpl=new TicketProductLinkView();
		
		RelevanceProduct rp=relevanceProductService.viewRelevance(comLink.getProd_id(),businessCode);
		
		LMMTicketProduct lp=lProductService.selectByProductId(rp.getProdId());
		tpl.setProd_id(lp.getProductId());
		tpl.setLowest_price(lp.getLowestPrice());
		
		
		/**
		 * 1.根据productId,从一条产品关联记录里拿到对应xc产品表详细信息
		 * 2.将xc产品详细信息存入TicketProductLink中
		 */
		if(rp.getProdIdXC()!=null&&rp.getProdIdXC()!=""&&rp.getProdNameXC()!=null&&rp.getProdNameXC()!=""){
			XCTicketProduct xcp=xcProductService.selectByProdId(rp.getProdIdXC());
			if(xcp!=null){
				tpl.setProd_id_xc(xcp.getProductId());
				tpl.setLowest_price_xc(xcp.getLowestPrice());
			}
		}
		/**
		 * 1.根据prodIdXC,从一条产品关联记录里拿到对应tn产品表详细信息
		 * 2.将tn产品详细信息存入TicketProductLink中
		 */
		if(rp.getProdIdTN()!=null&&rp.getProdIdTN()!=""&&rp.getProdNameTN()!=null&&rp.getProdNameTN()!=""){
			TNTicketProduct tnp=tnProductService.selectByProdId(rp.getProdIdTN());
			if(tnp!=null){
				tpl.setProd_id_tn(tnp.getProductId());
				tpl.setLowest_price_tn(tnp.getLowestPrice());
			}
		}
		/**
		 * 1.根据prodIdTC,从一条产品关联记录里拿到对应tc产品表详细信息
		 * 2.将tc产品详细信息存入TicketProductLink中
		 */
		if(rp.getProdIdTC()!=null&&rp.getProdIdTC()!=""&&rp.getProdNameTC()!=null&&rp.getProdNameTC()!=""){
			TCTicketProduct tcp=tcProductService.selectByProdId(rp.getProdIdTC());
			if(tcp!=null){
				tpl.setProd_id_tc(tcp.getProductId());
				tpl.setLowest_price_tc(tcp.getLowestPrice());
			}
		}
		/**
		 * 1.根据prodIdQNR,从一条产品关联记录里拿到对应qnr产品表详细信息
		 * 2.将qnr产品详细信息存入TicketProductLink中
		 */
		if(rp.getProdIdQNR()!=null&&rp.getProdIdQNR()!=""&&rp.getProdNameQNR()!=null&&rp.getProdNameQNR()!=""){
			QNRTicketProduct qnrp=qnrProductService.selectByProdId(rp.getProdIdQNR());
			if(qnrp!=null){
				tpl.setProd_id_qnr(qnrp.getProductId());
				tpl.setLowest_price_qnr(qnrp.getLowestPrice());
			}
		}

		/**
		 * 1.根据prodIdMT,从一条产品关联记录里拿到对应mt产品表详细信息
		 * 2.将mt产品详细信息存入TicketProductLink中
		 */
		if(rp.getProdIdMT()!=null&&rp.getProdIdMT()!=""&&rp.getProdNameMT()!=null&&rp.getProdNameMT()!=""){
			MTTicketProduct mtp=mtProductService.selectByProdId(rp.getProdIdMT());
			if(mtp!=null){
				tpl.setProd_id_mt(mtp.getProductId());
				tpl.setLowest_price_mt(mtp.getLowestPrice());
			}
		}
		result.put("prodPrice", tpl);
		return result;
	}
	public Map<String, Object> getResult() {
    	
        Map<String, Object> result = new HashMap<String, Object>();
        
        return result;
    }
	private void parseToLineDataVo(List<Map<String, String>> map,String companyCode,List<LineDataVO>priceList){
		String grab_time="";
		for(Map<String,String> data:map){
			if(data.get("insert_time")!=null){
				grab_time=data.get("insert_time").substring(0, 4)+"-"+data.get("insert_time").substring(4, 6)+"-"+data.get("insert_time").substring(6, 8);
			}else if(data.get("create_time")!=null){
				grab_time=data.get("create_time").substring(0, 4)+"-"+data.get("create_time").substring(4, 6)+"-"+data.get("create_time").substring(6, 8);
			}else if(data.get("updateTime")!=null){
				grab_time=data.get("updateTime").substring(0, 4)+"-"+data.get("updateTime").substring(4, 6)+"-"+data.get("updateTime").substring(6, 8);
			}
			
			if(data.get("sale_price")!=null&&!"".equals(data.get("sale_price"))){
				LineDataVO price = new LineDataVO();
				price.setTime(grab_time);
				price.setCategory(companyCode);
				price.setAmount(Double.valueOf(data.get("sale_price")));
				priceList.add(price);
			}else if(data.get("salePrice")!=null&&!"".equals(data.get("salePrice"))){
				LineDataVO price = new LineDataVO();
				price.setTime(grab_time);
				price.setCategory(companyCode);
				price.setAmount(Double.valueOf(data.get("salePrice")));
				priceList.add(price);
			}
		}
	}
}
